//
//  ViewController.m
//  03-UIGestureRecognizer
//
//  Created by 冀永金 on 16/7/18.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 手势力识别器
    // 分为7种
    // 轻拍
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self.view addGestureRecognizer:tap];
    tap.numberOfTapsRequired = 1;// 点击次数触发
    tap.numberOfTouchesRequired = 1;// 几个手指触发
    // 长按
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longAction:)];
    [self.view addGestureRecognizer:longPress];
    // 长按几秒后开始，默认是。5
    longPress.minimumPressDuration = 1;
    // 默认10像素 长按失败前 是最大的移动距离
    longPress.allowableMovement = 10;
    
//     轻扫
    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwip:)];
    [self.view addGestureRecognizer:swip];
    // direction 可以设置多个 前提对各方向所触发的行为要相同
    // 如果多个方向触发的行为不同，需要创建对个手势轻扫的手势
    swip.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
    // 创建ImageView添加轻扫换图
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 300, 400)];
    imageView.image = [UIImage imageNamed:@"123_10.jpg"];
    [self.view addSubview:imageView];
    UISwipeGestureRecognizer *swip1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(changePic:)];
    [imageView addGestureRecognizer:swip1];
    imageView.userInteractionEnabled = YES;
    UISwipeGestureRecognizer *swip2 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(changePic1:)];
    [imageView addGestureRecognizer:swip2];
    // 按位枚举 4个选项 1 1 1 1
    // 全选的情况 1 （4） 1（3）1 （2）1（0）
    swip2.direction = UISwipeGestureRecognizerDirectionLeft;
//     平移
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
    [imageView addGestureRecognizer:pan];
//     捏合
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
    [imageView addGestureRecognizer:pinch];
//     旋转 
    UIRotationGestureRecognizer *rota = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRota:)];
    [imageView addGestureRecognizer:rota];
//     屏幕边缘手势
    
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)tapAction:(UITapGestureRecognizer *)tap {
    NSLog(@"轻拍");
}
- (void)longAction:(UILongPressGestureRecognizer *)longPress {
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"开始");
    }
    else {
        NSLog(@"结束");
    }
}


- (void)handleSwip:(UISwipeGestureRecognizer *)swip {
    NSLog(@"轻扫");
}
- (void)changePic:(UISwipeGestureRecognizer *)swip {
        UIImageView *image = (UIImageView *)swip.view;
        image.image = [UIImage imageNamed:@"123_11.jpg"];
        [self.view addSubview:image];
    
}
- (void)changePic1:(UISwipeGestureRecognizer *)swip {
    UIImageView *image = (UIImageView *)swip.view;
    image.image = [UIImage imageNamed:@"123_10.jpg"];
    [self.view addSubview:image];
    
}
- (void)handlePan:(UIPanGestureRecognizer *)pan {
    if (pan.state == UIGestureRecognizerStateBegan) {
        
    }else if (pan.state == UIGestureRecognizerStateChanged) {
        
        // 平移手势，根据其他view上移动的过程，得到最终点
        CGPoint point = [pan translationInView:pan.view];
        NSLog(@"%@",NSStringFromCGPoint(point));
        // 根据point使pan.view 移动
        pan.view.transform =CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
        // pan移动的点置空
        [pan setTranslation:CGPointZero inView:pan.view];
    }else if (pan.state == UIGestureRecognizerStateEnded) {
       // pan.view.transform = CGAffineTransformIdentity;
    }
}


- (void)handlePinch:(UIPinchGestureRecognizer *)pinch {
    //修改imageView对象的transForm属性
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    pinch.scale = 1;

}

- (void)handleRota:(UIRotationGestureRecognizer *)rota {
    rota.view.transform = CGAffineTransformRotate(rota.view.transform, rota.rotation);
    // 旋转弧度
    rota.rotation = 0;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
